package data

import (
	"context"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"learn/k8s/internal/pkg/model"
)

func (d *Data) NameSpaceList(ctx context.Context) ([]*model.NameSpace, error) {
	var res []*model.NameSpace
	list, err := d.k8s.CoreV1().Namespaces().List(ctx, metav1.ListOptions{})
	if err != nil {
		return nil, err
	}
	if len(list.Items) > 0 {
		for _, item := range list.Items {
			res = append(res, &model.NameSpace{Name: item.Name, Status: string(item.Status.Phase), CreateTime: item.CreationTimestamp.Time})
		}
	}
	return res, nil
}
